<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">实验室名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="实验室名称"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">楼层信息</label>
        <el-select
          v-model="query.floor"
          clearable
          style="width: 185px;"
          placeholder="楼层信息"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option value="1" label="一楼" />
          <el-option value="2" label="二楼" />
          <el-option value="3" label="三楼" />
          <el-option value="4" label="四楼" />
          <el-option value="5" label="五楼" />
          <el-option value="6" label="六楼" />
        </el-select>
        <label class="el-form-item-label">实验室管理员</label>
        <el-select
          v-model="query.administrator"
          clearable
          style="width: 185px;"
          placeholder="实验室管理员"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        >
          <el-option
            v-for="item in admins"
            :key="item.administrator"
            :label="item.adminName"
            :value="item.administrator"
          />
        </el-select>
        <!-- <label class="el-form-item-label">实验室类型</label>
        <el-input
          v-model="query.labType"
          clearable
          placeholder="实验室类型"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        @open="openDialog"
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="房间号" prop="roomNumber">
            <el-input v-model="form.roomNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="楼层信息" prop="floor">
            <el-select
              v-model="form.floor"
              style="width: 370px;"
              placeholder="请选择楼层信息"
            >
              <el-option label="一楼" :value="1" />
              <el-option label="二楼" :value="2" />
              <el-option label="三楼" :value="3" />
              <el-option label="四楼" :value="4" />
              <el-option label="五楼" :value="5" />
              <el-option label="六楼" :value="6" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="实验室规范">
            <el-input v-model="form.standard" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item> -->

          <!-- <el-form-item label="实验室类型" prop="labType">
            未设置字典，请手动设置 Select
          </el-form-item> -->
          <el-form-item label="授权数量" prop="authNumber">
            <el-select
              v-model="form.authNumber"
              style="width: 370px;"
              placeholder="请选择授权数量"
            >
              <el-option label="0" value="0" />
              <el-option label="1" value="1" />
              <el-option label="2" value="2" />
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="实验室管理员" prop="admin_name">
            <el-input v-model="form.admin_name" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="管理员" prop="administrator">
            <el-select
              v-model="form.administrator"
              style="width: 370px;"
              clearable
              filterable
              :remote-method="getAdmins"
              placeholder="请选择管理员"
            >
              <el-option
                v-for="item in admins"
                :key="item.administrator"
                :label="item.adminName"
                :value="item.administrator"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="父实验室" prop="pid">
            <el-select
              v-model="form.pid"
              style="width: 370px;"
              clearable
              filterable
              :remote-method="getLabs"
              placeholder="请选择父实验室"
            >
              <el-option
                v-for="item in labs"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="实验室名称" />
        <el-table-column prop="floor" label="楼层信息">
          <template slot-scope="scope">
            <span v-if="scope.row.floor == 1">一楼</span>
            <span v-if="scope.row.floor == 2">二楼</span>
            <span v-if="scope.row.floor == 3">三楼</span>
            <span v-if="scope.row.floor == 4">四楼</span>
            <span v-if="scope.row.floor == 5">五楼</span>
            <span v-if="scope.row.floor == 6">六楼</span>
          </template>
        </el-table-column>
        <el-table-column prop="roomNumber" label="房间号" />
        <!-- <el-table-column prop="standard" label="实验室规范" /> -->
        <el-table-column prop="adminName" label="实验室管理员" />
        <!-- <el-table-column prop="labType" label="实验室类型" /> -->
        <el-table-column prop="authNumber" label="授权数量" />
        <el-table-column prop="pName" label="父实验室" />
        <el-table-column
          v-if="checkPer(['admin', 'lab:edit', 'lab:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudLab from "@/api/lab";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { get, findAdmin } from "@/api/lab";

const defaultForm = {
  id: null,
  name: null,
  floor: null,
  roomNumber: null,
  standard: null,
  adminName: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
  labType: 1,
  authNumber: null,
  administrator: null,
  pid: null
};
export default {
  name: "Lab",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "实验室",
      url: "api/lab",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudLab }
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "lab:add"],
        edit: ["admin", "lab:edit"],
        del: ["admin", "lab:del"]
      },
      rules: {
        name: [
          { required: true, message: "实验室名称不能为空", trigger: "blur" }
        ],
        floor: [
          { required: true, message: "楼层信息不能为空", trigger: "blur" }
        ],
        roomNumber: [
          { required: true, message: "房间号不能为空", trigger: "blur" }
        ],
        administrator: [
          { required: true, message: "实验室管理员不能为空", trigger: "change" }
        ],
        labType: [
          { required: true, message: "实验室类型不能为空", trigger: "blur" }
        ],
        authNumber: [
          { required: true, message: "实验室类型不能为空", trigger: "blur" }
        ]
      },
      queryTypeOptions: [
        { key: "name", display_name: "实验室名称" },
        { key: "floor", display_name: "楼层信息" },
        { key: "adminName", display_name: "实验室管理员" },
        { key: "labType", display_name: "实验室类型" }
      ],
      admins: [],
      labs: []
    };
  },
  created() {
    this.getLabs();
    this.getAdmins();
  },
  methods: {
    getLabs() {
      get()
        .then(response => {
          this.labs = response;
        })
        .catch(_ => {
          console.log(response);
        });
    },
    getAdmins() {
      findAdmin(this.searchForm)
        .then(response => {
          this.admins = response;
        })
        .catch(_ => {
          console.log(response);
        });
    },
    openDialog() {
      get()
        .then(response => {
          this.labs = response;
          if (this.form.name) {
            this.labs = this.labs.filter(item => {
              return item.name != this.form.name;
            });
          }
        })
        .catch(_ => {
          console.log(response);
        });
    },
    //  changeLab(id) {
    //       this.labs = this.labs.filter(item => {
    //         console.log(11111)
    //         return item.id != id
    //       })
    // },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.getAllLabs()
      return true;
    },
    [CRUD.HOOK.afterDelete]() {
      this.getAllLabs()
    },
    async getAllLabs(){
      const res1 = await get()
      this.$store.commit('hjuser/setAllLabs',res1)
    }
  }
};
</script>

<style scoped></style>
